using System;
using System.Collections.Generic;
using System.Text;

namespace Codex
{
    public class Entry : IComparable
    {
        public DateTime entryDate;
        public String entryText;
        public List<string> tagList = new List<string>();
        public List<FieldTag> fieldList = new List<FieldTag>();

        public Entry() { }


        public Entry(DateTime _entryDate, String _entryText)
        {
            setEntryDate(_entryDate);
            setEntryText(_entryText);
        }

        public void setEntryText(String _entryText) { entryText = _entryText; }
        public void setEntryDate(DateTime _entryDate) { entryDate = _entryDate; }
        public void setLabelList(List<string> _labelList) { tagList = _labelList; }
        public void setFieldList(List<FieldTag> _fieldList) { fieldList = _fieldList; }

        public String getEntryText() { return entryText; }
        public DateTime getEntryDate() { return entryDate; }
        public List<string> getLabelList() { return tagList; }
        public List<FieldTag> getFieldList() { return fieldList; }


        //Label Methods
        public void addLabel(string newLabel) { tagList.Add(newLabel);
        }
        public void removeLabel(string label) 
        {
            tagList.Remove(label);                                                                                                                                  
        }
        public void clearLabelList() { tagList.Clear(); }

        public bool hasLabel(string label) { return tagList.Contains(label); }

        //Field methods
        public void addField(string fieldName) 
        {
            foreach (FieldTag tempField in fieldList)
            {
                if (tempField.getFieldName().Equals(fieldName))
                    return;
            }

            if(!fieldName.Equals(""))
            fieldList.Add(new FieldTag(fieldName));

            Console.WriteLine("Field list:");
            foreach (FieldTag tempField in fieldList)
            {
                Console.WriteLine(tempField.getFieldName() + ":" + tempField.getFieldValue());
            }
        }

        public string getFieldValue(string fieldName)
        {
            foreach(FieldTag tempField in fieldList)
                {
                    if(tempField.getFieldName().Equals(fieldName))
                        return tempField.getFieldValue();
                }

            return null;
        }

        public void setFieldValue(string fieldName, string fieldValue)
        {
            //Console.WriteLine("Changing field: " + fieldName + " to " + fieldValue);
            foreach (FieldTag tempField in fieldList)
            {
                if (tempField.getFieldName().Equals(fieldName))
                {
                    tempField.setFieldValue(fieldValue);
                    Console.WriteLine("Changing field: " + fieldName + " to " + fieldValue);
                
                }
            }
        }


        public void removeBlankTags()
        {
            if(fieldList.Count > 0)
            fieldList.RemoveAll(fieldNameBlank);
        }

        public static bool fieldNameBlank(FieldTag tempTag)
        {
            if (tempTag.getFieldName() == null ||
                tempTag.getFieldName().Equals("")) return true;
            else return false;
        }


        public int CompareTo(Object obj)
        {
            if (obj is Entry)
            {
                Entry temp = (Entry)obj;

                return getEntryDate().CompareTo(temp.getEntryDate());
            }
            throw new ArgumentException("object is not an Entry");
        }

        public override String ToString()
        {
            String outputString = "";

            outputString += "\n\nDate: " + entryDate.ToString();
            outputString += "\n\n" + entryText;

            return outputString;
        }
    }
}
